package edu.stu.recyclerviewscrollbar

import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.SCROLL_STATE_DRAGGING
import androidx.recyclerview.widget.RecyclerView.SCROLL_STATE_IDLE
import edu.stu.recyclerviewscrollbar.adapter.ImgAdapter
import edu.stu.recyclerviewscrollbar.entity.ImageInfo
import edu.stu.recyclerviewscrollbar.scroll.ScrollBar


class MainActivity : AppCompatActivity() {


    private lateinit var imageList: ArrayList<ImageInfo>

    private lateinit var scrollBar: ScrollBar
    private lateinit var rv: RecyclerView


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initData()
    }

    private fun initData() {
        scrollBar = findViewById(R.id.scroll_bar)
        rv = findViewById(R.id.rv)
//        val linearLayoutManager = LinearLayoutManager(this, RecyclerView.HORIZONTAL, false)
        val layoutManager = GridLayoutManager(this, 2, GridLayoutManager.HORIZONTAL, false)
        rv.layoutManager = layoutManager
        imageList = ArrayList()
        for (i in 0 until 20) {
            imageList.add(ImageInfo(i.toString(), R.drawable.img))
        }
        rv.adapter = ImgAdapter(this, imageList)


        rv.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                super.onScrollStateChanged(recyclerView, newState)

            }

            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)
                /*val layoutManager = recyclerView.layoutManager as LinearLayoutManager?
                val firstVisibleItemPosition = layoutManager?.findFirstVisibleItemPosition()
                if (firstVisibleItemPosition != null && firstVisibleItemPosition != RecyclerView.NO_POSITION) {
                    // 当前显示的第一个 item 的位置索引
                    if (scrollBar.getInitialState()) {
                        val percent = firstVisibleItemPosition.toFloat() / imageList.size.toFloat()
                        Log.e("TAG", "onScrolled: "+percent )
                        scrollBar.animateTo(percent)
                    }

                } else {
                    Log.e("TAG", "RecyclerView is empty or no items are visible")
                }*/

                // 获取 RecyclerView 的水平滚动范围和水平滚动偏移
                val totalScrollRange = recyclerView.computeHorizontalScrollRange()
                val scrollOffset = recyclerView.computeHorizontalScrollOffset()
                val scrollExtent = recyclerView.computeHorizontalScrollExtent()
                val scrollPercent = if (totalScrollRange - scrollExtent > 0) {
                    scrollOffset.toFloat() / (totalScrollRange - scrollExtent).toFloat()
                } else {
                    0f
                }
                if (scrollBar.getInitialState()) {
                    Log.e("TAG", "onScrolled: "+scrollPercent )
                    scrollBar.animateTo(scrollPercent)
                }

            }
        })
    }
}